package com.hmall.search.consumer;

import com.hmall.search.service.SearchService;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component

public class SpringRabbitListener {


    @Autowired
    private SearchService searchService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "item.up"),
            exchange = @Exchange(name = "item.direct", type = ExchangeTypes.DIRECT),
            key = {"up"}

    ))
    public void itemUp(String id) {
        System.out.println("itemUp：【" + id + "】");
        searchService.saveItemById(Long.valueOf(id));


    }


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "item.below"),
            exchange = @Exchange(name = "item.direct", type = ExchangeTypes.DIRECT),
            key = {"below"}
    ))
    public void itemBelow(String id) {
        System.out.println("item.below：【" + id + "】");
        searchService.deleteItemById(Long.valueOf(id));
    }
}